package com.ruoyi.web.controller.crm;

import com.bonuo.crm.domain.Client;
import com.bonuo.crm.domain.Contact;
import com.bonuo.crm.service.ContactService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

//联系人Controller
@RestController
@RequestMapping("/crm/contact")
@Api(tags = "联系人管理")
public class ContactController extends BaseController {
    @Autowired
    private ContactService contactService;

    //查询联系人
    @ApiOperation(value = "查询联系人",httpMethod = "GET")
    @GetMapping("/list")
    public TableDataInfo list(Contact contact,Integer pageNum,Integer pageSize) {
        if(pageNum != null && pageSize != null){
            startPage();
        }
        List<Contact> contactList = contactService.selectContactList(contact);
        return getDataTable(contactList);
    }

    //添加联系人
    @ApiOperation(value = "添加联系人",httpMethod = "POST")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Contact contact) {
        AjaxResult add = contactService.insertContact(contact);
        return add;
    }

    //删除联系人
    @ApiOperation(value = "删除联系人",httpMethod = "DELETE")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        Integer delete = contactService.deleteContactById(id);
        return toAjax(delete);
    }

    //修改联系人
    @ApiOperation(value = "修改联系人",httpMethod = "PUT")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody Contact contact) {
        AjaxResult update = contactService.updateContact(contact);
        return update;
    }

    //导出联系人
    @ApiOperation(value = "导出联系人",httpMethod = "POST")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Contact contact) {
        List<Contact> list = contactService.selectContactList(contact);
        ExcelUtil<Contact> util = new ExcelUtil<Contact>(Contact.class);
        util.exportExcel(response, list, "联系人信息数据");
    }
}
